package cn.mulanbay.admin.api.persistent.service;

import cn.mulanbay.common.exception.PersistentException;
import cn.mulanbay.common.util.StringUtil;
import cn.mulanbay.persistent.dto.FastMenuDto;
import cn.mulanbay.persistent.entity.FastMenu;
import cn.mulanbay.persistent.mapper.FastMenuMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Author: fenghong
 * @Create : 2022/2/6
 */
@Service
public class FastMenuService extends ServiceImpl<FastMenuMapper, FastMenu> {

    @Autowired
    FastMenuMapper fastMenuMapper;

    public List<FastMenuDto> getUserFastMenu(Long userId){
        return fastMenuMapper.getUserFastMenu(userId);
    }

    /**
     * 保存用户快捷菜单
     * @param userId
     * @param functionIds
     */
    @Transactional(rollbackFor = PersistentException.class)
    public void saveFastMenu(Long userId,String functionIds){
        fastMenuMapper.deleteUserFastMenu(userId);
        if (StringUtil.isNotEmpty(functionIds)) {
            String[] ids = functionIds.split(",");
            int i = 0;
            for (String s : ids) {
                FastMenu fm = new FastMenu();
                fm.setUserId(userId);
                fm.setFunctionId(Long.valueOf(s));
                fm.setOrderIndex(i++);
                fastMenuMapper.insert(fm);
            }
        }
    }


}
